﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {

    public static GameManager instance;
    public static GameManager Instance
    {
        get{

            return instance;
        }
    }
    public Sprite noPeople;
    public Sprite hasPeople;
    public Sprite Friend;
    public Sprite Enemy;
    public GameObject _Start;
    public GameObject End;
    public int PlayerID;
    public Text resource;
    Timer timer;
    public float exploreTime = 5f;
    public float colonyTime = 5f;
    public float destoryTime = 3f;
    public GameObject signPrefab;
    
    private void Start()
    {
         
      //  sign.GetComponent<LineRenderer>().startColor = new Color(1, 1, 1);
    }
    private void Awake()
    {
        instance = this;
    }

    private void Update()
    {
        
    }

    public void ToObserve()
    {
        if (_Start == null)
        {
            Debug.Log("Out of Range");
            return;
        }
        //如果资源不够，退出
        float dis = (_Start.transform.position - End.transform.position).magnitude;
        float cost = dis * ControlCenter.Instance.baseResourceUse.Explore;
        GameObject Player = GameObject.Find("PlayerCenter").transform.Find("Player2(Clone)").gameObject;
        if (Player.GetComponent<PlayerAcion>().CurState.ResourceInStore < cost)
        {
            Debug.Log("Out of Resource");
            return;
        }
        else //否则，可以探索，资源减少
        {
            ChangeResource(Player,cost);
            
            timer = new Timer(exploreTime);
           
            timer.OnEnd += Observe;
            timer.Start();
            GameObject sign = GameObject.Instantiate(signPrefab);
            timer.sign = sign;
            Vector3[] Vects = { _Start.transform.position, End.transform.position };
           // sign.GetComponent<LineRenderer>().startColor = new Color(1, 1, 1);
            sign.GetComponent<LineRenderer>().SetPositions(Vects);
            // sign.GetComponent<LineRenderer>().endColor = new Color(0, 1, 1);
            sign.GetComponent<LineRenderer>().material.color = new Color(0, 1, 1);
            //Invoke("DestorySign", exploreTime);


            //判断星球的状态，如果无人，修改星球对玩家的状态，显示you人,如果无人，修改星球对玩家的状态，显示无人

        }
    }

    void DestorySign(GameObject sign)
    {
        GameObject.Destroy(sign);
    }

    void Observe(GameObject sign)
    {
        sign.SetActive(false);
        GameObject spriteTip = End.transform.Find("SpriteTip").gameObject;
        if (End.GetComponent<StarSystem>().Owner == 0)//无人殖民，显示探索
        {
            //显示无人的提示
            spriteTip.SetActive(true);
            spriteTip.GetComponent<SpriteRenderer>().sprite = noPeople;

            End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType = StarSystem.Visible.VisibleTypes.Explored;
            End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].ExplorCD = ControlCenter.Instance.speedAndTime.ExploreCD;
        }
        else if (End.GetComponent<StarSystem>().Owner != PlayerID) //有人殖民
        {
            //显示you人的提示
            spriteTip.SetActive(true);
            spriteTip.GetComponent<SpriteRenderer>().sprite = hasPeople;
            End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType = StarSystem.Visible.VisibleTypes.Uncomunicate;
            End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].ComunicateCD = ControlCenter.Instance.speedAndTime.ComubicatedCD;
            _Start.GetComponent<StarSystem>().VisibleState[End.GetComponent<StarSystem>().Owner - 1].ComunicateCD = ControlCenter.Instance.speedAndTime.ComubicatedCD;
            ControlCenter.Instance.Players[PlayerID - 1].GetComponent<Player>().CurState.EnemySystemCount += 1;
        }
    }

    void HideTip(GameObject tip)
    {
        tip.gameObject.SetActive(false);
    }
    public void ToColonize()
    {
        GameObject Player = GameObject.Find("PlayerCenter").transform.Find("Player2(Clone)").gameObject;
        //判断星球的状态是否为已探索，若是则进行下面步骤，否则debug退出
        //判断星球是有人还是无人状态，若是无人状态，可以直接殖民，若为有人，则还要判断
        //若是友人，可直接探索
        //若是敌人，debug退出
        GameObject spriteTip = End.transform.Find("SpriteTip").gameObject;
        float dis = (_Start.transform.position - End.transform.position).magnitude;
        float cost = dis * ControlCenter.Instance.baseResourceUse.Colony;
        if (Player.GetComponent<PlayerAcion>().CurState.ResourceInStore < cost) return;
        else
        {
            if (End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType == StarSystem.Visible.VisibleTypes.Unexplore)
            {
                Debug.Log("to explore before colonize");
                return;
            }

            if (End.GetComponent<StarSystem>().Owner == 0 && End.GetComponent<StarSystem>().State_ == StarSystem.States.origin)
            {
                timer = new Timer(colonyTime);
                timer.OnEnd += Colony;
                timer.Start();
                GameObject sign = GameObject.Instantiate(signPrefab);
                timer.sign = sign;
                Vector3[] Vects = { _Start.transform.position, End.transform.position };
               // sign.GetComponent<LineRenderer>().startColor = new Color(1, 1, 1);
                sign.GetComponent<LineRenderer>().SetPositions(Vects);
                sign.GetComponent<LineRenderer>().material.color = new Color(0, 1, 0);
                //  sign.GetComponent<LineRenderer>().endColor = new Color(0, 1, 0);


            }
            else if (End.GetComponent<StarSystem>().Owner != PlayerID)
            {
                //提示为敌人，不能殖民
                spriteTip.SetActive(true);
                spriteTip.GetComponent<SpriteRenderer>().sprite = Enemy;
                return;
            }
            else
            {
                //提示为友人，不用殖民
                spriteTip.SetActive(true);
                spriteTip.GetComponent<SpriteRenderer>().sprite = Friend;
                return;
            }
        }

    }

    void Colony(GameObject sign)
    {
        sign.SetActive(false);
        GameObject Player = GameObject.Find("PlayerCenter").transform.Find("Player2(Clone)").gameObject;
        float dis = (_Start.transform.position - End.transform.position).magnitude;
        float cost = dis * ControlCenter.Instance.baseResourceUse.Colony;
        ChangeResource(Player, cost);
        End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType = StarSystem.Visible.VisibleTypes.Owend;
        End.GetComponent<StarSystem>().State_ = StarSystem.States.colonized;
        End.GetComponent<StarSystem>().Owner = PlayerID;
        ControlCenter.Instance.Players[End.GetComponent<StarSystem>().Owner - 1].GetComponent<Player>().CurState.OwnedSystems += 1;
    }

    public void ToContact()
    {
        //判断星球的状态是否为已探索，若是则进行下面步骤，否则debug退出
        //如果资源不够，退出
        //否则，可以探索，资源减少
        //判断是敌人还是友人
        //如果是友人，修改星球对玩家的状态，显示友人
        //如果是敌人，修改星球对玩家的状态，显示敌人
    }
    public void ToDestroy()
    {
        timer = new Timer(destoryTime);
        timer.OnEnd += Destory;
        timer.Start();
        GameObject sign = GameObject.Instantiate(signPrefab);
        timer.sign = sign;
        Vector3[] Vects = { _Start.transform.position, End.transform.position };
       // sign.GetComponent<LineRenderer>().startColor = new Color(1, 1, 1);
        sign.GetComponent<LineRenderer>().SetPositions(Vects);
        sign.GetComponent<LineRenderer>().material.color = new Color(1, 0, 0);
        //  sign.GetComponent<LineRenderer>().endColor = new Color(1, 0, 0);


    }

    void Destory(GameObject sign)
    {
        sign.SetActive(false);
        GameObject Player = GameObject.Find("PlayerCenter").transform.Find("Player2(Clone)").gameObject;
        float dis = (_Start.transform.position - End.transform.position).magnitude;
        float cost = dis * ControlCenter.Instance.baseResourceUse.Destroy;
        ChangeResource(Player, cost);
        End.GetComponent<StarSystem>().State_ = StarSystem.States.destroyed;
        End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType = StarSystem.Visible.VisibleTypes.Explored;
        if (End.GetComponent<StarSystem>().Owner != 0)
        {
            ControlCenter.Instance.Players[End.GetComponent<StarSystem>().Owner - 1].GetComponent<Player>().CurState.OwnedSystems -= 1;
            if (End.GetComponent<StarSystem>().VisibleState[PlayerID - 1].VisibleType != StarSystem.Visible.VisibleTypes.Unexplore)
                ControlCenter.Instance.Players[PlayerID - 1].GetComponent<Player>().CurState.EnemySystemCount -= 1;
        }
        End.GetComponent<StarSystem>().Owner = 0;
        End.gameObject.SetActive(false);
    }

    public void ChangeResource(GameObject player,float cost)
    {
        GameObject Player = player;
        Player.GetComponent<PlayerAcion>().CurState.ResourceInStore -= cost;
        if(Player.GetComponent<PlayerAcion>().PlayerID==2)
        {
            resource.text = Player.GetComponent<PlayerAcion>().CurState.ResourceInStore.ToString();
        }
        
    }
}
